<template>
  <div>
    <!-- 1. 输入框 -->
    <div class="header-input">
      <el-input
        v-model="input"
        placeholder="请输入内容"
        @change="changeInfo"
      ></el-input>
      <el-button type="primary" plain>查 詢</el-button>
      <el-button type="primary" plain @click="addList">添 加</el-button>
    </div>
    <!-- 类目选择表单 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="规格ID" width="80"> </el-table-column>
      <el-table-column prop="itemCatId" label="类目ID" width="80">
      </el-table-column>
      <el-table-column prop="specsName" label="规格名称" width="180">
      </el-table-column>
      <el-table-column prop="paramData" label="规格参数" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编 辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删 除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页  -->
    <Pagination
      :total="total"
      :pageSize="pageSize"
      @changeCurrent="changeCurrent"
    />
    <!-- 弹框 -->
    <SpecDialog ref="mySpecDialog" />
  </div>
</template>

<script>
import Pagination from "../../components/Pagination/Pagination";
import SpecDialog from "./SpecDialog";
import { mapMutations } from "vuex";
export default {
  components: {
    Pagination,
    SpecDialog,
  },
  data() {
    return {
      tableData: [],
      pageSize: 1,
      total: 1,
      input: "",
    };
  },
  methods: {
    ...mapMutations("specModule", ["setTitle", "setSpec"]),

    /**
     *  编辑事件 --
     * 1.获得信息 ---
     * 2.进行修改 --
     * 3.传入数据
     */
    handleEdit(index, row) {
      console.log("编辑————当前元素信息", index, row);
      this.setSpec(row);
      this.setTitle("编辑规格");
      // 弹框显示
      this.$refs.mySpecDialog.$refs.inDialog.innerVisible = true;
    },
    /**
     * 删除本条信息
     */
    handleDelete(index, row) {
      console.log("删除--该条元素信息", index, row);
      this.$api
        .deleteCategory({
          id: row.id,
        })
        .then((res) => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        });
      this.http();
    },

    // 信息改变事件
    changeInfo() {},
    // 添加类目事件
    addList() {
      this.$refs.mySpecDialog.dialogVisible = true;
      this.title = "添加规格";
    },
    // 分页 -- 获得改变页码
    changeCurrent(val) {
      this.http(val);
    },
    http(page) {
      this.$api
        .selectItemParamAll({ page })
        .then((res) => {
          // console.log("Spec-index-table ", res.data);
          this.tableData = res.data;
          this.pageSize = res.pageSize;
          this.total = res.total;
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  created() {
    this.http();
  },
};
</script>

<style lang="less" scoped>
.header-input {
  display: flex;
  padding: 5px;
  input {
    margin: 0 5px;
  }
  button {
    margin: 0 5px;
  }
}
</style>